package de.fuberlin.inf.alp3.ue06;

import java.security.InvalidParameterException;

public class Triadic implements Expression {
    final Expression condition, then, otherwise;
    Triadic (Expression condition, Expression then, Expression otherwise) {
        if (condition == null || then == null || otherwise == null)
            throw new InvalidParameterException();
        this.condition = condition;
        this.then = then;
        this.otherwise = otherwise;
    }
    @Override
    public Expression eval() {
        try {
            boolean cond = (((Boolean) condition.eval()).getValue());
            return (cond) ? then.eval(): otherwise.eval();
        } catch (ClassCastException e) {
            throw new RuntimeException("First operand of a triadic must be boolean");
        }
    }
}